package controller

import (
	"github.com/gin-gonic/gin"
	jsoniter "github.com/json-iterator/go"
	"github.com/wonderivan/logger"
	"ucenter/app/model"
	"ucenter/app/service"
)

/***********内部接口**************/
/**
 * 操作余额，结算扣分
 * param: *gin.Context c
 */
func Balance(c *gin.Context) {
	data, err := c.GetRawData()
	if err != nil {
		logger.Error("Internal_Balance_GetData | data=%v", string(data))
		c.JSON(500, &map[string]string{"msg": "get data error"})
		return
	}
	if data == nil {
		c.JSON(500, &map[string]string{"msg": "data is empty!"})
		return
	}
	logger.Debug("Internal_Balance_info | data=%v", string(data))

	balanceRequest := new(model.BalanceRequest)
	err = jsoniter.Unmarshal(data, balanceRequest)
	if err != nil {
		c.JSON(500, &map[string]string{"msg": "json Unmarshal error!"})
		return
	}
	uBList, err := service.GetBalanceService().OperateBalance(balanceRequest, string(data))
	if err != nil {
		c.JSON(500, &map[string]string{"code": "500", "msg": err.Error()})
		return
	}
	c.JSON(200, &map[string]interface{}{"code": "200", "msg": "success", "data": uBList})
	return
}

/**
 * 返回用户余额
 * param: *gin.Context c
 */
func GetBalance(c *gin.Context) {
	data, err := c.GetRawData()
	if err != nil {
		logger.Error("Internal_GetBalance_GetData | data=%v", string(data))
		c.JSON(500, &map[string]string{"msg": "get data error"})
		return
	}
	if data == nil {
		c.JSON(500, &map[string]string{"msg": "data is empty!"})
		return
	}
	balanceReq := new(model.GetUserBalance)
	err = jsoniter.Unmarshal(data, balanceReq)
	if err != nil {
		c.JSON(500, &map[string]string{"msg": "json Unmarshal error!"})
		return
	}
	list, err := service.GetBalanceService().GetBalance(balanceReq)
	if err != nil {
		c.JSON(500, &map[string]string{"msg": "db error!"})
		return
	}
	c.JSON(200, &map[string]interface{}{"code": "200", "msg": "success", "data": list})
	return
}
